package com.woniu.service.impl;

import com.woniu.dto.ProcessDischargeDTO;
import com.woniu.entity.Bed;
import com.woniu.entity.InpatientDischargeApp;
import com.woniu.entity.InpatientDischargeRecord;
import com.woniu.entity.InpatientRecord;
import com.woniu.mapper.*;
import com.woniu.service.DischargeService;
import com.woniu.utils.ResponseEnum;
import com.woniu.utils.ResponseUtil;
import com.woniu.utils.UserContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.ParseException;
import java.util.Date;

@Service
@Transactional
public class DischargeServiceImpl implements DischargeService {
    @Resource
    private BedMapper bedMapper;
    @Resource
    private InpatientDischargeAppMapper inpatientDischargeAppMapper;
    @Resource
    private InpatientRecordMapper inpatientRecordMapper;
    @Resource
    private InpatientDischargeRecordMapper dischargeRecordMapper;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ResponseUtil processDischarge(ProcessDischargeDTO dto) throws ParseException {
        Integer currentNurseId = UserContext.ContextUserId();

        // 检查出院申请单的状态是否为 '1' (已结算/待确认)
        InpatientDischargeApp app = inpatientDischargeAppMapper.selectByPrimaryKey(Long.valueOf(dto.getId()));

        if (app == null) {
            return ResponseUtil.get(ResponseEnum.FAIL, "办理失败：出院申请单不存在");
        }
        if (app.getOaStatus() == 0) { //
            return ResponseUtil.get(ResponseEnum.FAIL, "办理失败：该患者尚未在收费站完成出院结算！");
        }
        if (app.getOaStatus() == 2) { //
            return ResponseUtil.get(ResponseEnum.FAIL, "办理失败：该患者已办理过出院");
        }

        // (校验通过，状态为 1，执行原逻辑)

        // 1. 修改住院表 (inpatient_record) 状态为 2 (已出院)
        InpatientRecord recordUpdate = new InpatientRecord();
        recordUpdate.setHzCaseId(dto.getPatientId());
        recordUpdate.setHzStatus(2); // 2 = 已出院
        recordUpdate.setHzEnddate(new Date());
        // (注意: updateStatusByPatientId 需要修改为按 hz_case_id 和 hz_status=1 查询)
        inpatientRecordMapper.updateStatusByPatientId(recordUpdate); //

        // 2. 修改病床表 (bed) 状态为 0 (空闲)
        Bed bedUpdate = new Bed();
        bedUpdate.setId(dto.getBedId());
        bedUpdate.setStatus(0); // 0 = 空闲
        bedMapper.updateByPrimaryKeySelective(bedUpdate); //

        // 3. 修改出院申请表 (inpatient_discharge_app) 状态为 2 (已完成)
        InpatientDischargeApp appUpdate = new InpatientDischargeApp();
        appUpdate.setId(dto.getId());
        appUpdate.setOaStatus(2); // 2 = 已完成
        inpatientDischargeAppMapper.updateByPrimaryKeySelective(appUpdate); //

        // 4. 添加记录到出院表 (inpatient_discharge_record)
        InpatientDischargeRecord dischargeRecord = new InpatientDischargeRecord();
        dischargeRecord.setCyCaseId(dto.getPatientId());
        dischargeRecord.setCyWorkerId(dto.getDoctorId());
        dischargeRecord.setCyBedId(dto.getBedId());
        dischargeRecord.setCyEnddate(new Date());
        dischargeRecordMapper.insertSelective(dischargeRecord); //

        return ResponseUtil.get(ResponseEnum.OK, "办理出院成功");
    }
}
